var app = getApp();
var api = require('../../config/api.js');
var utils = require('../../utils/util.js');

Page({
  data: {
    areaList: [],
    selectedArea: null
  },
  
  onLoad: function (options) {
    this.loadAreaData();
  },
  
  loadAreaData: function() {
    var that = this;
    // 这里应该调用获取区域列表的API
    var params = new Object();
    utils.request(api.GetAreaList, params).then(function(res) {
      if (res.code == 1) {
        that.setData({
          areaList: res.data
        });
      }
    }).catch(function(err) {
      console.log('获取区域列表失败:', err);
      // 模拟数据
      that.setData({
        areaList: [
          { dept_id: '1', dept_name: '行政楼' },
          { dept_id: '2', dept_name: '教学楼A栋' },
          { dept_id: '3', dept_name: '教学楼B栋' },
          { dept_id: '4', dept_name: '实验楼' },
          { dept_id: '5', dept_name: '图书馆' }
        ]
      });
    });
  },
  
  selectArea: function(e) {
    var area = e.currentTarget.dataset.area;
    this.setData({
      selectedArea: area
    });
    
    // 跳转到访客登记页面，传递区域信息
    business.navigateTo({
      url: '../guest/guest?dept_id=' + area.dept_id + '&dept_name=' + encodeURIComponent(area.dept_name)
    });
  },
  
  onShow: function() {
    // 页面显示时的逻辑
  }
})